package cn.zch.soft.util.task.zhihu.collect;
import cn.zch.soft.model.zhihu.collections.ZchCollections;
import cn.zch.soft.util.http.okhttpclient.OkHttpClientUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
/**
 * https://www.zhihu.com/api/v4/people/codetimer/collections?offset=0&limit=100
 *
 * @author zchcpywin10
 */
@Slf4j
public class TaskZchCollections {
  public static void main(String[] args) {
    getCollections();
  }
  /**
   * Init.
   */
  public static void getCollections() {
    try {
      // OkHttpClient client = new OkHttpClient().newBuilder().build()
      // https://www.zhihu.com/api/v4/people/codetimer/collections?
      // include=data[*].updated_time,answer_count,follower_count,creator,description,is_following,comment_count,
      // created_time;data[*].creator.vip_info
      // &offset=0
      // &limit=100
      //      Request request = new Request.Builder().url("https://www.zhihu.com/api/v4/people/codetimer/collections?offset=0&limit=100")
      //        //
      //        .method("GET", null)
      //        //.addHeader("Cookie", "_xsrf=7c899559-ce43-45b9-a927-9f72b8d0699e;
      //        // _zap=f6047737-238e-4c18-b7a0-5a62a2bf7056;
      //        // d_c0=\"ALAdtV6tphOPTq3hSk4zXX9gewMYyApLreY=|1630383033\";
      //        // KLBRSID=e42bab774ac0012482937540873c03cf|1630464299|1630464269")
      //        .build();
      //      Response response = OK_HTTP_CLIENT.newCall(request).execute();
      String url = "https://www.zhihu.com/api/v4/people/codetimer/collections?offset=0&limit=100";
      String collectionsResult = OkHttpClientUtil.doRequest(url, "GET", null, null);
      ZchCollections zchCollections = null;
      if (null != collectionsResult) {
        // {"errCode": 200, "errString": "OK"}
        System.out.println("zchCollections = " + zchCollections);
        zchCollections = JSON.parseObject(collectionsResult, ZchCollections.class);
        System.out.println("zchCollections = " + zchCollections);
        System.out.println("总共 = " + zchCollections.getPaging().getTotals());
        System.out.println("getData = " + JSON.toJSONString(zchCollections.getData()));
        List<ZchCollections.DataDTO> data = zchCollections.getData();
        int i = 0;
        for (ZchCollections.DataDTO datum : data) {
          i = i + 1;
          String title = datum.getTitle();
          // System.out.println(i + " title = " + title)
          System.out.println(title);
        }
      }
    }
    catch (Exception e) {
      log.error(e.getLocalizedMessage(), e);
    }
  }
}
